package com.ajaxdemo.china;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ajaxdemo.util.DbDao;

/**
 * Servlet implementation class City
 */
@WebServlet("/City_xml")
public class City_xml extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		String father = request.getParameter("provinceID");
		String sql = "select cityID, city from hat_city where father = "
				+ father + ";";
		ResultSet rs = DbDao.queryDb(sql);
		StringBuffer result = new StringBuffer();
		try {
			result.append("<citys>");
			result.append("<city><name>请选择</name></city>");
			while (rs.next()) {
				result.append("<city>");
				result.append("<id>" + rs.getString(1));
				result.append("</id>");
				result.append("<name>" + rs.getString(2));
				result.append("</name>");
				result.append("</city>");
			}
			result.append("</citys>");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		//System.out.println(result.toString());
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/xml");
		PrintWriter out = response.getWriter(); // 发送到客户端
		out.println(result.toString());
		out.flush();
		out.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
